/**
 * LocationAction.java
 * 
 * 
 * lssrc.com
 * 2013-01-08
 */
package com.lssrc.pams.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.lssrc.pams.domain.Location;
import com.lssrc.pams.service.LocationService;
import com.opensymphony.xwork2.ActionSupport;

@Controller("locationAction")
@Scope("prototype")
public class LocationAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private Integer parentId;
	private String msg;
	private LocationService locationService;
	private Location location;
	private Location parentLocation;
	private List<Location> locations;

	public String add() {
		name = name.trim();
		if (name.length() == 0 || name == null) {
			msg = "[{result:'数据格式不正确'}]";
			return SUCCESS;
		}
		if (locationService.save(new Location(name, parentId))) {
			msg = "[{result:'success'}]";
		} else {
			msg = "[{result:'error'}]";
		}
		return SUCCESS;
	}

	public String delete() {
		location = new Location(id);
		if (locationService.findById(location) == null) {
			msg = "[{result:'您要删除的信息不存在'}]";
			return SUCCESS;
		}
		if (locationService.findByParent(location).size() != 0) {
			msg = "[{result:'您要删除的目录下还有其他数据'}]";
			return SUCCESS;
		}
		if(locationService.delete(location)) {
			msg = "[{result:'success'}]";
		} else {
			msg = "[{result:'error'}]";
		}
		return SUCCESS;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "locationService")
	public void setLocationService(LocationService locationService) {
		this.locationService = locationService;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public Location getParentLocation() {
		return parentLocation;
	}

	public void setParentLocation(Location parentLocation) {
		this.parentLocation = parentLocation;
	}

	public List<Location> getLocations() {
		return locations;
	}

	public void setLocations(List<Location> locations) {
		this.locations = locations;
	}

}
